
$(document).ready(function(){
    // render
    loadData();
    // event
    bindEventEdit();
});

function loadData() {
    var _maSize = ISS.SESSION.getSession('session_maSize');

    var size = {};
    size.maSize = _maSize;

    ISS.BACKEND.request({
        url: ISS.SESSION.getSession('contextPath') + '/service/admin/editProductSize',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        type: 'POST',
        dataType: 'json',
        data: JSON.stringify(size),
        success: function (data) {
            var responseContent = data.responseObject;
            loadDataComplete(responseContent);
        }
    });

    ISS.BACKEND.request({
        url: ISS.SESSION.getSession('contextPath') + '/service/admin/getAllCategoryInProductSize',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        type: 'GET',
        dataType: 'json',
        success: function (data) {
            var responseContent = data.responseObject;
            mapAllProductCategoryToView(responseContent);
        }
    });

}

function loadDataComplete(size){
    ISS.DOM.setVal('#RegeditFormSize_code', size.maSize);
    ISS.DOM.setVal('#RegeditFormSize_name', size.tenSize);
    ISS.DOM.setVal('#RegeditFormSize_categorieslist', size.loaiDanhMuc.maLoai);
}

function bindEventEdit(){
    ISS.EVENT.bindEvent('#RegeditFormEdit_size_Modified', 'click', function () {
        sendInfoEdit();
    });
}

function sendInfoEdit() {
    var size = {};
    size.maSize = ISS.DOM.getVal('#RegeditFormSize_code');
    size.tenSize = ISS.DOM.getVal('#RegeditFormSize_name');

    var category = {};
    category.maLoai = ISS.DOM.getVal('#RegeditFormSize_categorieslist_option');
    size.loaiDanhMuc = category;

    ISS.BACKEND.request({
        url: ISS.SESSION.getSession('contextPath') + '/service/admin/registerSizeEdit',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        type: 'POST',
        dataType: 'json',
        data: JSON.stringify(size),
        success: function (data) {
            var responseContent = data.responseObject;
            sendInfoEditComplete(responseContent);
        }
    });
}

function sendInfoEditComplete(color) {
    if(color) {
        alert('Cập nhật thành công');
        ISS.WINDOW.location('/jsp/admin/ad-product-size-common.jsp');
    }
    else{
        alert('Cập nhật không thành công');
    }
}

function mapAllProductCategoryToView(productcategory) {
    var sizeLDM = ISS.DOM.getVal('#RegeditFormSize_categorieslist');
    var n = '';
    var html = '<option value="default_value">...</option>';
    for(var i=0; i<productcategory.length; i++) {
        if (productcategory[i].maLoai == sizeLDM){
            n = 'selected="selected"';
        }
        else{
            n = '';
        }
        html += '<option value="' + productcategory[i].maLoai + '" ' + n + '  >' + productcategory[i].tenLoai + '</option>';
    }
    ISS.DOM.html('#RegeditFormSize_categorieslist_option', html);
}
